<?php 
require_once('functions.php');
require_once('config.php');

$db = mysqli_connect("localhost",'lr12aas' , 'u3CJFB4r', 'dblr12aas');
$getQuestions = mysqli_query($db, "select stem from sq_questions");
$questions = mysqli_fetch_array($getQuestions);  
$getAnswers = mysqli_query($db, "select CorrectAnswer, Answer2, Answer3, Answer4 from sq_answers");
$answers = mysqli_fetch_array($getQuestions);
$stem = $questions;
  

$array = mysqli_fetch_array($getAnswers); 
if (!isset($_POST['submitter'])) {
if (isset($_POST['register'])) { // they want to register
    $username = trim(strip_tags(stripslashes($_POST['username'])));
    if (ini_get('magic_quotes_gpc')) {
        $username = stripslashes($username);
    }
} else { //they don't want to register
    $random = rand(1,1000);
    $username = 'Anon'. $random;
} 
$num = 0;
} else {
	    $last = false;

$num = (int) $_POST['num'];
$postedanswer = str_replace("_"," ",$_POST['answers']);
if ($postedanswer == $answers[$num]['0']) {
    $_SESSION['score']++;
    $_SESSION['correct'][] = $postedanswer;

} else {
    $_SESSION['incorrect'][] = $postedanswer;

}
if ($num < count($questions)/*-1*/) {
    $num++;
} else {
    $last = true;
    $_SESSION['finished'] = 'yes';

}
}

$_SESSION['user'] = $username; // username
	echo "<p class='smiling'>Username: " .  $_SESSION['user'] . "</p>";	
$_SESSION['score'] = 0; // score set to 0
	echo "<p class='smiling'>Score: " .  $_SESSION['score'] . "</p>";	
$_SESSION['correct'] = array(); // to hold the user's correct answers
$_SESSION['incorrect'] = array(); // to hold the user's incorrect answers
$_SESSION['finished'] = 'no'; // they haven't finished the quiz yet
	echo "<p class='smiling'>Finished: " .  $_SESSION['finished'] . "</p>";
	
	echo "<p class='smiling'>Num: " . $num . "</p>";	
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="styles/styles.css" type="text/css" />
<title>mcq</title>
</head>
<body>
<p class='smiling'>Incorrect Answers: <?php echo print_r(array_values($_SESSION['incorrect']))?></p>
<p class='smiling'>Correct Answers: <?php echo print_r(array_values($_SESSION['correct']))?></p>
<div id="wrapper">
<div id="intro">
<?php if(isset($_SESSION['user'])) echo "<h4>Current tester:{$_SESSION['user']}</h4>"; ?>
</div><!--intro-->
<div id="quiz">
<?php if (!$last) { //display the form  ?>
<h2>QUESTION  <?php echo $num+1; ?>:</h2>
<p><?php echo $stem[$num]; ?></p>
<form id="questionBox" method="post" action="test.php">
<ul>
<?php
$pattern = ' ';
$replace = '_';
shuffle_assoc($answers[$num]);
foreach ($answers[$num] as $answer) {
    $answer2 = str_replace($pattern,$replace,$answer);
    echo "<li><input type=\"radio\" id=\"$answer2\" value=\"$answer2\" name=\"answers\" />\n";
    echo "<label for=\"$answer2\">$answer</label></li>\n";
}
?>
</ul>
<p><input type="hidden" name="num" value="<?php echo $num; ?>" />
<input type="hidden" name="submitter" value="TRUE" />
<input type="submit" id="submit" name="submit" value="Submit Answer" /></p>
</form>
<?php } else { 
echo "<h2 id=\"score\">{$_SESSION['user']}, your final score is:</h2>\n
<h3>{$_SESSION['score']}/20</h3><h4>Verdict:</h4>";
if($_SESSION['score'] <= 5) echo "<p id=\"verdict\">Feedback 1</p>\n";
if(($_SESSION['score'] > 5) && ($_SESSION['score'] <= 10)) echo "<p id=\"verdict\">Feedback 2</p>\n";
if(($_SESSION['score'] > 10) && ($_SESSION['score'] <= 15)) echo "<p id=\"verdict\">Feedback 3</p>\n";
if($_SESSION['score'] > 15) echo "<p id=\"verdict\">Feedback 4</p>";
echo "<p id=\"compare\"><a href=\"results2.php\">See how you compare!</a></p>";
}?>
</div><!--quiz-->
</div><!--wrapper-->
</body>
</html>